package com.tianhui.consignortwofei.mvp.ui.fragment;

import android.os.Bundle;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.widget.itemView.SettingItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.constant.MessageType;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.MessageModel;
import com.tianhui.consignortwofei.mvp.model.enty.message.MessageQualityInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.MessageListActivity;
import com.tianhui.consignortwofei.other.EventWhat;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * @author fgs
 * @date 2020/9/9
 * @description
 **/
public class MessageFragment extends BaseFragment {

    @BindView(R.id.fragment_message_systemMessageSettingItemView)
    SettingItemView mSystemMessageSettingItemView;
    @BindView(R.id.fragment_message_activeMessageSettingItemView)
    SettingItemView mActiveMessageSettingItemView;
    @BindView(R.id.fragment_message_orderMessageSettingItemView)
    SettingItemView mOrderMessageSettingItemView;
    private MessageModel mMessageModel;

    public MessageFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected CommonView createView() {
        return null;
    }

    @Override
    protected CommonPresenter createPresenter() {
        return null;
    }

    @Override
    protected void init() {

        mMessageModel = new MessageModel();


    }

    @Override
    protected void onUserVisible() {
        super.onUserVisible();
        requestMessageQuality(false);
        LogUtil.print("MessageFragment onUserInvisible");
    }

    private void requestMessageQuality(boolean isDialog) {
        mMessageModel.messageQualityModel(getContext(), isDialog, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<MessageQualityInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<MessageQualityInfo>> commonResponse) {
                try {
                    if (commonResponse.getCode() == 20000) {
                        List<MessageQualityInfo> list = commonResponse.getData();
                        if (list != null && list.size() > 0) {
                            for (MessageQualityInfo qualityInfo : list) {
                                if (qualityInfo.type == MessageType.SYSTEM) {
                                    mSystemMessageSettingItemView.setBadgeNumber(qualityInfo.quality);
                                } else if (qualityInfo.type == MessageType.ACTIVE) {
                                    mActiveMessageSettingItemView.setBadgeNumber(qualityInfo.quality);
                                } else if (qualityInfo.type == MessageType.ORDER) {
                                    mOrderMessageSettingItemView.setBadgeNumber(qualityInfo.quality);
                                }
                            }
                        } else {
                            mSystemMessageSettingItemView.setBadgeNumber(0);
                            mActiveMessageSettingItemView.setBadgeNumber(0);
                            mOrderMessageSettingItemView.setBadgeNumber(0);
                            EventBusManage.refreshMessagePoint("");
                        }
                    } else {
                        LogUtil.print(commonResponse.getMsg());
                    }
                }catch (Exception e){
                    LogUtil.print("fei",e.toString());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

                LogUtil.print(e.message);
            }
        });

    }

    @OnClick(R.id.fragment_message_systemMessageSettingItemView)
    public void systemMessage() {
        Bundle bundle = new Bundle();
        bundle.putInt(KeyValue.KEY_TYPE, MessageType.SYSTEM);
        openActivity(MessageListActivity.class, bundle);
    }

    @OnClick(R.id.fragment_message_activeMessageSettingItemView)
    public void activeMessage() {
        Bundle bundle = new Bundle();
        bundle.putInt(KeyValue.KEY_TYPE, MessageType.ACTIVE);
        openActivity(MessageListActivity.class, bundle);
    }

    @OnClick(R.id.fragment_message_orderMessageSettingItemView)
    public void orderMessage() {
        Bundle bundle = new Bundle();
        bundle.putInt(KeyValue.KEY_TYPE, MessageType.ORDER);
        openActivity(MessageListActivity.class, bundle);
    }


    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (EventWhat.valueOf(event.getWhat())) {
            case EVENT_REFRESH_CONSIGNOR_INFO:
                requestMessageQuality(false);
                break;
        }
    }


}
